package messangerServer;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;

public class ServerUnicastReadWorker extends Thread
{
	private ObjectInputStream in;
	private ServerBackend parent;
	private int port;
	public boolean quit = false;
	
	public ServerUnicastReadWorker (ServerBackend parent, int port, InputStream in) throws IOException
	{
		this.parent = parent;
		this.in = new ObjectInputStream(in);
		this.port = port;
	}
	
	public void run()
	{	
		Object o = null;

		try
		{
			while(!quit)
			{
				o = in.readObject();
				System.out.println("Server read work: object in!");
				parent.readWorkerCallback(o, port);
			}
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			parent.clientDisconnect(port);
			quit = true;
			try
			{
				in.close();
			} catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}//end of public void run()
	
	
}
